﻿// <copyright file="Translate.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>Translate implementation</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

  /// <summary>
  /// Translates between Names and IDs
  /// </summary>
  public class Translate
  {
    /// <summary>
    /// Get the link ID for a network link collections
    /// </summary>
    /// <param name="network">a network</param>
    /// <param name="linkName">link name</param>
    /// <returns>the link ID if a single link is found otherwise 0</returns>
    public static int GetLinkIDFromName(Network network, int linkName)
    {
      var qrygetroots =
          from p in network.Links
          where p.LinkName == linkName
          select p;

      if (qrygetroots.Count() != 1)
      {
        return 0;
      }
      else
      {
        return qrygetroots.ElementAt(0).Id;
      }
    }

    /// <summary>
    /// Get the link name for a network link collections
    /// </summary>
    /// <param name="network">a network</param>
    /// <param name="linkId">a link ID</param>
    /// <returns>the link Name if linkId exists, otherwise 0</returns>
    public static int GetLinkNameFromID(Network network, int linkId)
    {
      if (network.Links.Contains(linkId))
      {
        return network.Links[linkId].LinkName;
      }
      else
      {
        return 0;
      }
    }

    /// <summary>
    /// Get the node ID for a network link collections
    /// </summary>
    /// <param name="network">a network</param>
    /// <param name="nodeName">node name</param>
    /// <returns>the node ID if a single node is found otherwise 0</returns>
    public static int GetNodeIDFromName(Network network, string nodeName)
    {
      var qrygetroots =
          from p in network.Nodes
          where p.NodeName == nodeName
          select p;

      if (qrygetroots.Count() != 1)
      {
        return 0;
      }
      else
      {
        return qrygetroots.ElementAt(0).Id;
      }
    }
  }
}
